package com.itheima.sc.impl.sms;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsHomeBrandApi;
import com.itheima.sc.mapper.sms.SmsHomeBrandMapper;
import com.itheima.sc.pojo.sms.HomeBrand;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandImpl implements SmsHomeBrandApi {

    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    //添加首页推荐品牌
    public void addBrand(List<HomeBrand> homeBrands) {
        //遍历集合
        for (HomeBrand homeBrand : homeBrands) {
            homeBrandMapper.insert(homeBrand);
        }
    }

    //批量删除首页推荐品牌
    public void deleteBrands(List<Long> ids) {
        homeBrandMapper.deleteBatchIds(ids);
    }

    //分页查询首页推荐的品牌
    public PageResult selectBrandList(Integer page, Integer pageSize, Integer recommendStatus, String brandName) {
        //构建分页查询条件
        Page<HomeBrand> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<HomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, HomeBrand::getRecommendStatus, recommendStatus);
        wrapper.like(StrUtil.isNotBlank(brandName), HomeBrand::getBrandName, brandName);
        //排序
        wrapper.orderByDesc(HomeBrand::getSort);
        //查询请求
        ipage = homeBrandMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //批量修改品牌的状态
    public void updateBrandStatus(List<Long> ids, Integer status) {
        //遍历数据
        for (Long id : ids) {
            HomeBrand homeBrand = new HomeBrand();
            homeBrand.setId(id);
            homeBrand.setRecommendStatus(status);
            homeBrandMapper.updateById(homeBrand);
        }
    }

    //修改品牌的排序
    public void updateBrandSort(HomeBrand homeBrand) {
        homeBrandMapper.updateById(homeBrand);
    }

   /* //查询brandId
    public List<Long> selectBrands(List<Long> ids) {
        List<HomeBrand> homeBrands = homeBrandMapper.selectBatchIds(ids);
        return CollUtil.getFieldValues(homeBrands,"brandId",Long.class);
    }*/
}
